package com.stars.easyms.logging.properties;

import com.plumelog.log4j2.appender.KafkaAppender;
import com.plumelog.log4j2.appender.RedisAppender;
import com.stars.easyms.base.util.SpringBootUtil;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

import static com.stars.easyms.logging.constant.EasyMsLoggingConstants.*;

/**
 * <p>className: EasyMsLoggingInitialization</p>
 * <p>description: EasyMs日志模块初始化类</p>
 *
 * @author guoguifang
 * @version 1.7.0
 * @date 2020/12/1 9:59 上午
 */
public final class EasyMsLoggingInitialization {

    public static void init(EasyMsLoggingProperties easyMsLoggingProperties) {
        initLogColor(easyMsLoggingProperties);
        initPlumeLog(easyMsLoggingProperties);
    }

    private static void initLogColor(EasyMsLoggingProperties easyMsLoggingProperties) {
        EasyMsLoggingProperties.LogColor logColor = easyMsLoggingProperties.getLogColor();
        boolean enabled = logColor.isEnabled();
        EasyMsLoggingLogColorHelper.setEnabled(enabled);
        if (!enabled) {
            EasyMsLoggingLogColorHelper.setLevelColorMap(null);
            return;
        }

        Map<String, String> levelColorMap = new HashMap<>(8);
        levelColorMap.putAll(DEFAULT_LEVEL_COLOR_MAP);
        Map<String, String> levelMap = logColor.getLevel();
        if (!levelMap.isEmpty()) {
            levelMap.forEach((key, value) ->
                    levelColorMap.put(key.trim().toUpperCase(), value.trim().toUpperCase()));
        }
        EasyMsLoggingLogColorHelper.setLevelColorMap(levelColorMap);
    }

    private static void initPlumeLog(EasyMsLoggingProperties easyMsLoggingProperties) {
        EasyMsLoggingProperties.PlumeLog plumeLog = easyMsLoggingProperties.getPlumeLog();

        // 设置plumeLog是否激活，如果plumeLog未激活则appender都不激活
        if (!plumeLog.isEnabled()) {
            EasyMsLoggingPlumeLogHelper.setEnabled(false);
            return;
        }
        EasyMsLoggingPlumeLogHelper.setEnabled(true);

        String appName = plumeLog.getAppName() != null ? plumeLog.getAppName() : SpringBootUtil.getApplicationName();
        EasyMsLoggingPlumeLogHelper.setAppName(appName);

        EasyMsLoggingProperties.PlumeLog.Redis redis = plumeLog.getRedis();
        String redisHost = redis.getHost();
        EasyMsLoggingPlumeLogHelper.setRedisAppenderEnabled(!StringUtils.isEmpty(redisHost));

        EasyMsLoggingProperties.PlumeLog.Kafka kafka = plumeLog.getKafka();
        String kafkaHosts = kafka.getHosts();
        EasyMsLoggingPlumeLogHelper.setKafkaAppenderEnabled(!StringUtils.isEmpty(kafkaHosts));

        // redisAppender和kafkaAppender只能有一个
        if (EasyMsLoggingPlumeLogHelper.getRedisAppender() == null && EasyMsLoggingPlumeLogHelper.getKafkaAppender() == null) {
            if (!StringUtils.isEmpty(redisHost)) {
                EasyMsLoggingPlumeLogHelper.setRedisAppender(RedisAppender.createAppender(PLUME_LOG_REDIS_APPENDER_NAME, appName,
                        redisHost, redis.getPort(), redis.getAuth(), plumeLog.getMaxCount(),
                        plumeLog.getRunModel(), plumeLog.getExpand(), redis.getDb(), plumeLog.getLogQueueSize(),
                        plumeLog.getThreadPoolSize(), null, null));
                // 3.4以上版本
//                EasyMsLoggingPlumeLogHelper.setRedisAppender(RedisAppender.createAppender(PLUME_LOG_REDIS_APPENDER_NAME, appName,
//                        redisHost, redis.getPort(), redis.getAuth(), plumeLog.getMaxCount(),
//                        plumeLog.getRunModel(), plumeLog.getExpand(), redis.getDb(), plumeLog.getLogQueueSize(),
//                        plumeLog.getThreadPoolSize(), plumeLog.isCompressor(), redis.getModel(), redis.getMasterName(), null, null));
            } else if (!StringUtils.isEmpty(kafkaHosts)) {
                EasyMsLoggingPlumeLogHelper.setKafkaAppender(KafkaAppender.createAppender(PLUME_LOG_KAFKA_APPENDER_NAME, appName,
                        kafkaHosts, null, plumeLog.getExpand(), plumeLog.getRunModel(), plumeLog.getMaxCount(),
                        plumeLog.getLogQueueSize(), plumeLog.getThreadPoolSize(), null, null));
                // 3.4以上版本
//                EasyMsLoggingPlumeLogHelper.setKafkaAppender(KafkaAppender.createAppender(PLUME_LOG_KAFKA_APPENDER_NAME, appName,
//                        kafkaHosts, null, plumeLog.getExpand(), plumeLog.getRunModel(), plumeLog.getMaxCount(),
//                        plumeLog.getLogQueueSize(), plumeLog.getThreadPoolSize(), plumeLog.isCompressor(), null, null));
            }
        }
    }

    private EasyMsLoggingInitialization() {
    }
}
